package com.itfreer.gather;

import android.content.Context;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.View;

import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.itfreer.arcgis.LayerUtils;

import java.util.List;

/**
 * 拉框选择监听类
 */
public class SelectTouchListener extends DefaultMapViewOnTouchListener
{
    Graphic g;
    MapView  map;
    Point p0 = null;
    SimpleFillSymbol sfs;
    GraphicsOverlay lineGraphicOverlay = null;
    List<FeatureLayer> featureLayers;
    public SelectTouchListener(Context arg0, MapView arg1, List<FeatureLayer> featureLayers)
    {
        super(arg0, arg1);
        map = arg1;

        sfs = new SimpleFillSymbol();
        sfs.setColor(Color.TRANSPARENT);
        sfs.setOutline(new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID,Color.GRAY,2));
        lineGraphicOverlay = new GraphicsOverlay();
        mMapView.getGraphicsOverlays().add(lineGraphicOverlay);
        this.featureLayers = featureLayers;
    }

    @Override
    public boolean onTouch(View view,MotionEvent event)
    {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                if(lineGraphicOverlay == null || lineGraphicOverlay.getGraphics() ==null)
                    return  super.onTouch(view,event);
                lineGraphicOverlay.getGraphics().clear();
                p0 = map.screenToLocation(new android.graphics.Point(Math.round(event.getX()), Math.round(event.getY())));
                Point p2 = map.screenToLocation(new android.graphics.Point(Math.round(event.getX()+5), Math.round(event.getY()+5)));
                Envelope envelope = new Envelope(p0, p2);
                g = new Graphic(envelope, sfs);
                lineGraphicOverlay.getGraphics().add(g);
                return  true;
            case MotionEvent.ACTION_MOVE:
                if(lineGraphicOverlay == null || lineGraphicOverlay.getGraphics() ==null)
                    return  super.onTouch(view,event);

                if(g == null)
                    return  super.onTouch(view,event);

                Point p3 = map.screenToLocation(new android.graphics.Point(Math.round(event.getX()), Math.round(event.getY())));
                Envelope envelope2 = new Envelope(p0, p3);
                g.setGeometry(envelope2);
                return  true;
            case MotionEvent.ACTION_UP:
                if(featureLayers != null && featureLayers.size() !=0 && lineGraphicOverlay!=null)
                {
                    if (g != null && g.getGeometry() != null)
                    {
                        for (Integer i = 0; i < featureLayers.size(); i++)
                        {
                            featureLayers.get(i).clearSelection();
                            LayerUtils.SelectFeatures(g.getGeometry().getExtent(), map, featureLayers.get(i));
                        }
                    }
                }
                lineGraphicOverlay.getGraphics().clear();
                mMapView.getGraphicsOverlays().remove(lineGraphicOverlay);
                lineGraphicOverlay = null;
                p0 = null;
                mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(mMapView.getContext(),mMapView));
                return true;
            default:
                return  super.onTouch(view,event);
        }
    }
}
